#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2016-2018, Niklas Hauser
# Copyright (c) 2017, Fabian Greif
#
# This file is part of the modm project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# -----------------------------------------------------------------------------

def init(module):
    module.name = ":board:disco-f469ni"
    module.description = FileReader("module.md")
    # Revisions = [b-03, b-02, b-01]

def prepare(module, options):
    if not options[":target"].partname.startswith("stm32f469nih"):
        return False

    module.depends(
        ":architecture:clock",
        ":architecture:memory",
        ":debug",
        ":driver:ft6x06",
        ":platform:clock",
        ":platform:rcc",
        ":platform:core",
        ":platform:gpio",
        ":platform:i2c:1",
        ":platform:uart:3",
        ":platform:usb:fs",
        ":ui:display",
        ":math:utils")
    return True

def build(env):
    env.outbasepath = "modm/src/modm/board"
    a1, a2 = [0x2a, 0x38]
    if env[":disco-f469ni"] == "b-03":
        a1, a2 = a2, a1
    env.substitutions = {
        "with_logger": True,
        "with_assert": env.has_module(":architecture:assert"),
        "touch_address": a1,
        "touch_address2": a2,
    }
    env.template("../board.cpp.in", "board.cpp")
    env.copy("board_display.cpp")
    env.copy("board_dsi.cpp")
    env.copy("board_init.cpp")
    env.copy("board_otm8009a.cpp")
    env.copy("board_sdram.cpp")
    env.template("board.hpp.in")
    env.collect(":build:openocd.source", "board/stm32f469discovery.cfg")

    env.collect(":platform:cortex-m:linkerscript.memory", linkerscript_memory)
    env.collect(":platform:cortex-m:linkerscript.sections", linkerscript_sections)
    env.collect(":platform:cortex-m:linkerscript.table_extern.zero", linkerscript_extern_zero)
    env.collect(":platform:cortex-m:linkerscript.table_extern.copy", linkerscript_extern_copy)
    env.collect(":platform:cortex-m:linkerscript.table_extern.heap", linkerscript_extern_heap)


# =============================================================================
linkerscript_memory = """\
SDRAM (rwx) : ORIGIN = 0xC0000000, LENGTH = 16M
"""

linkerscript_sections = """\
__sdram_start = ORIGIN(SDRAM);
__sdram_end = ORIGIN(SDRAM) + LENGTH(SDRAM);

.data_sdram :
{
    __data_sdram_load = LOADADDR(.data_sdram);
    __data_sdram_start = .;
    *(.data_sdram .data_sdram.*)
    . = ALIGN(4);
    __data_sdram_end = .;
} >SDRAM AT >FLASH

.bss_sdram (NOLOAD) :
{
    __bss_sdram_start = .;
    *(.bss_sdram .bss_sdram.*)
    . = ALIGN(4);
    __bss_sdram_end = .;
} >SDRAM

.noinit_sdram (NOLOAD) :
{
    __noinit_sdram_start = .;
    *(.noinit_sdram .noinit_sdram.*)
    . = ALIGN(4);
    __noinit_sdram_end = .;
} >SDRAM

.heap_sdram (NOLOAD) :
{
    __heap_sdram_start = .;
    *(.heap_sdram .heap_sdram.*)
    . = ORIGIN(SDRAM) + LENGTH(SDRAM);
    __heap_sdram_end = .;
} >SDRAM
"""

linkerscript_extern_zero = """\
LONG(__bss_sdram_start)
LONG(__bss_sdram_end)
"""

linkerscript_extern_copy = """\
LONG(__data_sdram_load)
LONG(__data_sdram_start)
LONG(__data_sdram_end)
"""

linkerscript_extern_heap = """\
LONG(0x801f)
LONG(__heap_sdram_start)
LONG(__heap_sdram_end)
"""
